package com.service.ljl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dao.ljl.Deliverdao;
import com.dao.ljl.deliverdaomybatis;
import com.dao.ljl.orderdao;
import com.dao.ljl.ordermybatis;
import com.dao.zy.ProductDao;
import com.entity.Deliver;
import com.entity.Deliverrecord;
import com.entity.Order;
import com.entity.Product;

@Service
@Transactional(rollbackFor = ServiceException.class)
public class deliverservice {
	@Autowired
	private Deliverdao deliverdao;
	@Autowired
	private orderdao orderdao;
	@Autowired
	private deliverdaomybatis deliverdaomybatis;
	@Autowired
	private ordermybatis ordermybatis;
	@Autowired
	private ProductDao productDao;

	public void add(Map map) {
		Deliver deliver = (Deliver) JSONObject.parseObject(map.get("deliver").toString(), Deliver.class);
		Long orid = JSONObject.parseObject(map.get("orid").toString(), Long.class);
		Long proid = JSONObject.parseObject(map.get("proid").toString(), Long.class);
		deliver.setOrder(orderdao.findOne(orid));
		deliver.setProduct(productDao.findOne(proid));
		deliverdao.save(deliver);
	}

	public Map<Object, Object> findall(int pagesize, int currage) {
		List<Deliver> deliver = deliverdaomybatis.findall();
		for (int i = 0; i < deliver.size(); i++) {
			BigDecimal count = new BigDecimal(0);
			if (deliver.get(i).getDeliverrecords().size() > 0) {
				for (int x = 0; x < deliver.get(i).getDeliverrecords().size(); x++) {
					count = count.add(deliver.get(i).getDeliverrecords().get(x).getDrNum());
				}

			}
			deliver.get(i).setRecordnum(count);
		}
		Map<Object, Object> map = new HashMap<>();
		int start = (currage - 1) * pagesize;
		int finish = currage * pagesize;
		if (finish > deliver.size()) {
			finish = deliver.size();
		}
		map.put("totol", deliver.size());
		map.put("delivers", deliver.subList(start, finish));
		return map;
	}

	public List<Order> findone(Long id) {
		List<Order> li = ordermybatis.findorderbycusid(id);
		return li;
	}

	public List<Order> findoneall(Long id) {
		List<Order> li = ordermybatis.findorderbycusidall(id);
		return li;
	}

	public void delete(Long id) {
		deliverdao.delete(id);
	}

	public void deletes(List<Long> ids) {
		for (int i = 0; i < ids.size(); i++) {
			deliverdao.delete(ids.get(i));
		}

	}

	public Map<Object, Object> findrecord(int pagesize, int currage) {
		List<Deliverrecord> Deliverrecord = deliverdaomybatis.findrecord();

		Map<Object, Object> map = new HashMap<>();
		int start = (currage - 1) * pagesize;
		int finish = currage * pagesize;
		if (finish > Deliverrecord.size()) {
			finish = Deliverrecord.size();
		}
		map.put("totol", Deliverrecord.size());
		map.put("Deliverrecord", Deliverrecord.subList(start, finish));
		return map;
	}
	
	public Map productlist(Long id) {
		Map map=new HashMap<>();
		List<Deliver> li=deliverdaomybatis.findproductbyrecord(id);
		map.put("deliver", li);
		return map;
	}
	
	public List<Product> findbyname(String name){
		return deliverdaomybatis.findbypname(name);
	}
	
	public void adddeliver(Map map) {
		Long id=JSONObject.parseObject(map.get("orId").toString(),Long.class);
		
		List<Deliver> delivers=(List<Deliver>)JSONObject.parseArray((map.get("deliver")).toString(),Deliver.class);
		for(int i=0;i<delivers.size();i++) {
			delivers.get(i).setOrder(orderdao.findOne(id));
			deliverdao.save(delivers.get(i));
		}
	}

}
